package com.kd.spring.unit;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kd.spring.configuration.myConfiguration;
import com.kd.spring.dao.OrderDao;
import com.kd.spring.dao.ProductDao;
import com.kd.spring.service.HelloService;
import com.kd.spring.service.PersonService;
import com.kd.spring.service.UserService;

public class SpringTest {
	
	@Test
	public void instance(){
//		ApplicationContext  ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});//多个配置文件
		ApplicationContext  ctx = new ClassPathXmlApplicationContext("beans.xml");//单个配置文件
		PersonService personService = (PersonService)ctx.getBean("personService");
		personService.save();

	}

	@Test
	public void test() {
		ApplicationContext  ctx = new ClassPathXmlApplicationContext("application.xml");//单个配置文件
		HelloService helloService = (HelloService)ctx.getBean("helloService");
		helloService.sayHello();
	}
	
	@Test
	//测试bean的自动装配
	public void testAutoWire(){
		ClassPathXmlApplicationContext  ctx = new ClassPathXmlApplicationContext("application.xml");//单个配置文件
		UserService userService = (UserService)ctx.getBean("userService");
		userService.regiest();
		ctx.close();
	}
	
	@Test
	//测试spring 3.0 的新特性
	public void testSprign3Configuration(){
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
		applicationContext.register(myConfiguration.class);
		applicationContext.refresh();
		
		ProductDao productDao = (ProductDao) applicationContext.getBean("productDao");
		System.out.println(productDao);
		
		OrderDao orderDao  = (OrderDao)applicationContext.getBean("orderDao");
		System.out.println(orderDao);
	}

}
